import time

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import unittest
from ui_zdh.jiuxian.read_excel import read_excel
from ddt import ddt, data, unpack, file_data

# 初始化
mm = read_excel('./login.xlsx', 'Sheet1')


@ddt()
class JiuXian(unittest.TestCase):

    def setUp(self) -> None:
        # 打开浏览器
        self.driver = webdriver.Firefox()
        # 双击打开百度页面
        self.driver.get('https://www.jiuxian.com/')
        time.sleep(1)
        # 窗口最大化
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def tearDown(self) -> None:
        time.sleep(10)
        self.driver.quit()

    # @unittest.skip
    @data(*mm.read())
    @unpack
    def test_jiuxian_01(self, caseid, casetitle, casesj1, casesj2, caseexp):
        print(caseid + casetitle + caseexp)

        time.sleep(1)
        if '登录成功' in caseexp:
            # 点击请登录
            self.driver.find_element(By.CLASS_NAME, 'headerLogin').click()
            # 输入手机号
            self.driver.find_element(By.ID, 'userName').send_keys(casesj1)
            # 输入密码
            self.driver.find_element(By.ID, 'password').send_keys(casesj2)
            # 强制等待 用户点击图片输入验证码
            time.sleep(20)
            # 点击登录按钮
            self.driver.find_element(By.NAME, 'bi_Usuallogin').click()
            # 点击确认
            self.driver.find_element(By.ID, 'bindSub').click()
            # 断言1：验证登录成功后断言：hi,jxw563943535
            time.sleep(2)
            dy1 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div/p/span').text
            self.assertEqual('jxw563943535', dy1)
        else:
            # 点击请登录
            self.driver.find_element(By.CLASS_NAME, 'headerLogin').click()
            # 输入手机号
            self.driver.find_element(By.ID, 'userName').send_keys(casesj1)
            # 输入密码
            self.driver.find_element(By.ID, 'password').send_keys(casesj2)
            # 强制等待 用户点击图片输入验证码
            time.sleep(20)
            # 点击登录按钮
            self.driver.find_element(By.NAME, 'bi_Usuallogin').click()
            if '用户名或密码错误' in caseexp:
                # 断言2:
                time.sleep(2)
                tt = self.driver.find_element(By.CSS_SELECTOR, '#validcaptcha > div.mistakeTip').text
                exp1 = str(caseexp).split('"')[1]
                print(exp1)
                self.assertEqual(exp1, tt)
            else:
                # 断言3：
                time.sleep(2)
                kk = self.driver.find_element(By.CSS_SELECTOR, '#loginform > div > div > div > div > div.loginCon > div.loginType.loginUsual > div.Frame.enterFrame.wrong > div.mistakeTip').text
                exp2 = str(caseexp).split('"')[1]
                print(exp2)
                self.assertEqual(exp2, kk)

if __name__ == '__main__':
    unittest.main()
